//
//  NewFeaturesController.swift
//  Weibo项目
//
//  Created by vn on 16/7/23.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit

class NewFeaturesController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

    
        setUpUI()
        
        
    }
    
    
    func setUpUI(){
        
        //添加
        view.addSubview(collectionView)
        
        
        //设置约束
        collectionView.frame = view.frame
        
        
    }
    
    
    //懒加载collectionView
    lazy var collectionView:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        view.bounces = false
        view.pagingEnabled = true
        
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false

        
        
        
        view.delegate = self
        view.dataSource = self
        
        //注册cell
        view .registerClass(SHNewfeatureCell.self, forCellWithReuseIdentifier:"cell")
        
        
        
        
        
        
       return view
        
    }()
    
    

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        
        return 4
        
    }

    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SHNewfeatureCell
        
        cell.imageIndex = indexPath.item
        
        cell.backgroundColor = RandomColor()
        
        return cell
        
        
}

    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        // 代表是最后一个cell
        if index == 3 {
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! SHNewfeatureCell
           
            
            
            cell.experience_btn.hidden = false
            
            //按钮动画
            cell.setAnimation()
            
        }
    }
    
    
}


//自定义cell
class SHNewfeatureCell:UICollectionViewCell{
        
        
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        
    }
    
    //定义一个属性,用来赋值图片
    var imageIndex:Int = 0 {
        
        didSet{
            
            icon_img.image = UIImage(named:"new_feature_\(imageIndex + 1)")
           
            //cell会重用,只要给cell赋值图片,就影藏Btn
            experience_btn.hidden = true

        }
    }
    
 
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
        
    
    
    //设置子控制器
    func setUpUI(){
         contentView.addSubview(icon_img)
        contentView.addSubview(experience_btn)
        
        
        //设置约束
         icon_img.frame = contentView.frame
        
        
        experience_btn.snp_makeConstraints { (make) -> Void in
            
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView.snp_bottom).offset(-100)
            
         }
    }
    //设置按钮动画
    func setAnimation(){
        
        self.experience_btn.userInteractionEnabled = false
        experience_btn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.experience_btn.transform = CGAffineTransformIdentity
            
            
            }) { (_) -> Void in
             
              self.experience_btn.userInteractionEnabled = true
                
        }
        
    }
    
    //按钮监听方法
    func clickButton(){
        
        //通知appDelegate修改rootViewCollection
         NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVC, object:"newFeature")
        
    }
    
    
    //懒加载控件
    lazy var icon_img:UIImageView = UIImageView()
    
    lazy var experience_btn:UIButton = {
        
        let btn = UIButton()
        
        
        btn.setTitle("点击立即体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_finish_button"), forState: UIControlState.Normal)
        btn.sizeToFit()
        
        btn.layer.borderColor = ThemeColor.CGColor
        btn.layer.borderWidth = 2
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.hidden = true
        
        //添加监听方法
        btn.addTarget(self, action:"clickButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        
       return btn
        
    }()
    
    
    
        
}
    
    
    
    
    
    
    
    
    


